package com.zhaoyd.uaademo.core.orm.extensions.provider;

import com.zhaoyd.uaademo.core.orm.extensions.mapper.UpdateListMapper;
import org.apache.ibatis.mapping.MappedStatement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

import java.util.Set;

/**
 * mysql批量更新Provider
 * @author zhaoyd
 * @date 2018-04-16 10:38
 **/
public class UpdateListProvider extends MapperTemplate {
    private static final Logger log = LoggerFactory.getLogger(UpdateListMapper.class);
    public UpdateListProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }
    /**
     * 批量插入
     * @param ms
     */
    public String updateListByPrimaryKeySelective(MappedStatement ms) {
        final Class<?> entityClass = getEntityClass(ms);
        //获取主键
        Set<EntityColumn> pkColumnList = EntityHelper.getPKColumns(entityClass);
        //无主键
        if(pkColumnList.size()<1){
            log.error("{}找不到主键", entityClass.getName());
            return "";
        }
        EntityColumn pkColumn = pkColumnList.iterator().next();
        //开始拼sql
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
        sql.append("<trim prefix=\"set\" suffixOverrides=\",\">");
        //获取全部列
        Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
        for (EntityColumn column : columnList) {
            if (!column.isId() && column.isInsertable()) {
                sql.append("<trim prefix=\" ").append(column.getColumn()).append(" = case\" suffix=\"end,\">");
                sql.append("<foreach collection=\"list\" item=\"item\" index=\"index\">");

                sql.append("<choose>");

                sql.append("<when test=\"");
                sql.append("item").append(".").append(column.getProperty()).append(" != null\">");
                sql.append(" when ").append(pkColumn.getColumn()).append(" = #{item.").append(pkColumn.getProperty()).append("} then ");
                sql.append(" #{item.").append(column.getProperty()).append("} ");
                sql.append("</when>");

                sql.append("<otherwise>");
                sql.append(" when ").append(pkColumn.getColumn()).append(" = #{item.").append(pkColumn.getProperty()).append("} then ");
                sql.append(column.getColumn());
                sql.append("</otherwise>");

                sql.append("</choose>");
                sql.append("</foreach>");
                sql.append("</trim>");
            }
        }
        sql.append("</trim>");
        sql.append("where ").append(pkColumn.getColumn()).append(" in");
        sql.append(" <foreach collection=\"list\" index=\"index\" item=\"item\" separator=\",\" open=\"(\" close=\")\">");
        sql.append(" #{item.").append(pkColumn.getProperty()).append("} ");
        sql.append("</foreach>");
        return sql.toString();
    }

}
